package com.yoomiito.app.widget.dialog;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yoomiito.app.R;
import com.yoomiito.app.utils.ImageLoader;

public class SystemTipDialog extends BaseDialog {

    private boolean canBack = true;
    private boolean mCanceledOnTouchOutside = true;
    private ImageView mCancelTv;
    private ImageView adView;
    private String adUrl;
    private View.OnClickListener mClickListener;
    private View.OnClickListener mCancelClickListener;
    private int mIvWidth = -1;
    private int mIvHeight = -1;
    public SystemTipDialog(@NonNull Context context) {
        this(context, 0);
    }

    public SystemTipDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected SystemTipDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_system_1, null);
        adView = view.findViewById(R.id.bg_ad);
        if (mIvWidth!=-1&&mIvHeight!=-1){
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) adView.getLayoutParams();
            lp.width = mIvWidth;
            lp.height = mIvHeight;
        }

        ImageLoader.getInstance().into(mContext,adUrl,adView);
        mCancelTv = view.findViewById(R.id.cancel);
        mCancelTv.setOnClickListener(v -> dismiss());
        setCanceledOnTouchOutside(mCanceledOnTouchOutside);
        return view;
    }

    @Override
    public void onBackPressed() {
        if (canBack)super.onBackPressed();
    }

    public SystemTipDialog setClickListener(View.OnClickListener clickListener) {
        mClickListener = clickListener;
        return this;
    }

    public SystemTipDialog setmCanceledOnTouchOutside(boolean mCanceledOnTouchOutside) {
        this.mCanceledOnTouchOutside = mCanceledOnTouchOutside;
        return this;
    }

    public SystemTipDialog setAdUrl(String adUrl) {
        this.adUrl = adUrl;
        if (adView!=null){
            ImageLoader.getInstance().into(mContext,adUrl,adView);
        }
        return this;
    }

    public void setIvWidthHeight(int width,int height){
        this.mIvWidth = width;
        this.mIvHeight = height;
    }

    /**
     * 取消按钮默认点击后取消弹窗。如果点击取消按钮后有其他操作，请设置监听事件。
     * @param mCancelClickListener
     * @return
     */
    public SystemTipDialog setmCancelClickListener(View.OnClickListener mCancelClickListener) {
        this.mCancelClickListener = mCancelClickListener;
        return this;
    }
}
